package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.model.Harmless;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;


/**
 * Created by niufei on 2017/3/24.
 *
 */
@Repository
public class HarmlessDao extends MyBatisDao<Harmless>{

    /**
     * 养殖端的无害化分页查询
     * @param offset 页码
     * @param limit 页数
     * @param criteria 参数
     * @return 分页的对象
     */
    public Paging<Harmless> farmPagingHarmless(Integer offset , Integer limit, Map<String ,Object> criteria){

        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("farmCount"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<Harmless> data = getSqlSession().selectList(this.sqlId("farmPagingHarmless"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 屠宰端的无害化分页查询
     * @param offset 页码
     * @param limit 页数
     * @param criteria 参数
     * @return 分页的对象
     */
    public Paging<Harmless> butcheryPagingHarmless(Integer offset , Integer limit, Map<String ,Object> criteria){
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("butcheryCount"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<Harmless> data = getSqlSession().selectList(this.sqlId("butcheryPagingHarmless"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     *
     * @param offset 页码
     * @param limit 页数
     * @param criteria 参数
     * @return Paging<Harmless>
     */
    public Paging<Harmless> pagingHarmlessAll(Integer offset , Integer limit, Map<String ,Object> criteria){
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("countAll"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<Harmless> data = getSqlSession().selectList(this.sqlId("pagingAll"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     *
     * @param id 主键id
     * @return 状态
     */
    public Integer findStatusById(Long id){
        return getSqlSession().selectOne(sqlId("findStatusById"),id);
    }

    /**
     * 返回当前单据的编码的最大值
     * @param number 除去流水号的前缀
     * @return string
     */
    public String findCurrentMaxNumber(String number){
        return getSqlSession().selectOne(sqlId("findCurrentMaxNumber"),number);
    }

    /**
     * 根据主键id查询单据的状态  和  处理方式
     * @param id 主键id
     * @return Map<String , Integer>
     */
    public Map<String , Integer> findStatusAndTreatment(Long id){
        return getSqlSession().selectOne(sqlId("findStatusAndTreatment"),id);
    }

    /**
     * 求和 无害化 占比图
     * @param map 参数
     * @return List<Map<String , Integer>>
     */
    public List<Map<String , Integer>> findSumIsLocal(Map<String,Object> map){
        return getSqlSession().selectList(sqlId("findSumIsLocal"),map);
    }

    /**
     * 求和 无害化占比图
     * @param map 参数
     * @return List<Map<String ,Integer>>
     */
    public List<Map<String ,Integer>> findSumReportType(Map<String , Object> map){
        return getSqlSession().selectList(sqlId("findSumReportType"),map);
    }

    /**
     * 七天的数据
     * @param map 参数
     * @return List<Map<String ,Object>>
     */
    public List<Map<String ,Object>> findAWeekSumReportType(Map<String , Object> map){
        return getSqlSession().selectList(sqlId("findAWeekSumReportType"),map);
    }

    /**
     * 七天的数据
     * @param map 参数
     * @return List<Map<String ,Object>>
     */
    public List<Map<String ,Object>> findAWeekSumIsLocal(Map<String , Object> map){
        return getSqlSession().selectList(sqlId("findAWeekSumIsLocal"),map);
    }

    /**
     * 通用条件查询
     * @param offset
     * @param limit
     * @param criteria
     * @return
     */
    public Paging<Harmless> pagingCommon(Integer offset , Integer limit, Map<String ,Object> criteria){
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(this.sqlId("count1"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<Harmless> data = getSqlSession().selectList(this.sqlId("pagingCommon"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 无害化来源
     * @return
     */
    public List<String> reportType(){

        return getSqlSession().selectList(sqlId("reporttype"));
    }

    public List<Map> harmlessranking(Map criteria){
        return getSqlSession().selectList(sqlId("harmlessranking"),criteria);
    }

    public List<Map> harmlesspic(Map criteria){
        return getSqlSession().selectList(sqlId("harmlesspic"),criteria);
    }


    public List<Harmless> selectByFacotryIds(Map criteria){
        return getSqlSession().selectList(sqlId("selectByfactoryIds"),criteria);
    }

    /**
     * 溯源
     * @param criteria 参数
     * @return Harmless
     */
    public Harmless findHarmlessByMarkNumberAndReportId(Map<String ,Object> criteria){
        return getSqlSession().selectOne(sqlId("findHarmlessByMarkNumberAndReportId"),criteria);
    }
}
